/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package petstore.view.action.admin;

import petstore.view.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.DynaValidatorActionForm;
import petstore.business.IAdminFacade;
import petstore.model.Usuario;
import petstore.util.Util;

/**
 *
 * @author Tom Hayden
 */
public class AutenticarUsuarioAction extends CustomDispatchAction {

    private IAdminFacade adminFacade;

    public void setAdminFacade(IAdminFacade adminFacade) {
        this.adminFacade = adminFacade;
    }
    
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        return mapping.findForward("mesmaPagina");
    }

    public ActionForward autenticar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String forward = "mesmaPagina";

        DynaValidatorActionForm f = (DynaValidatorActionForm) form;
        ActionErrors errors = f.validate(mapping, request);

        if (errors.isEmpty()) {
            
            forward = HOME;
            Usuario usuario = new Usuario();
            BeanUtils.copyProperties(usuario, f);

            boolean senhaValida = this.adminFacade.autenticarUsuario(usuario.getLogin(), usuario.getSenha());
            if (senhaValida) {
                usuario = this.adminFacade.getUsuario(usuario.getLogin());
                this.setUserInSession(request, usuario);

                //TODO: recuperar perfil e acessos

                // Se senha expirou, direcionar para UC Altera Senha
                if (usuario.isSenhaExpirada()) {
                    errors.add("error", new ActionMessage("autenticarusuario.error.senhaexpirada"));
                    forward = "senhaExpirada";
                }

            } else {
                errors.add("error", new ActionMessage("autenticarusuario.error.falhaautenticacao"));
                forward = "mesmaPagina";
            }
            
        }
        if (!errors.isEmpty()) {
            request.setAttribute(Globals.ERROR_KEY, errors);
        }

        return mapping.findForward(forward);
    }
    
    public ActionForward efetuarLogoff(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        this.removeUserOfSession(request);
        request.getSession().invalidate();
        
        return mapping.findForward("logoff") ;
        
    }
}
